const BaseService = require("./BaseService.js");
const md5 = require("md5-node");
const AppConfig = require("../config/AppConfig.js");

class AdminInfoService extends BaseService{
    constructor(){
        super();
        this.currentTableName = this.tableMap.admin_info;
    }
    // async add({admin_name,admin_sex,admin_tel,admin_pwd,admin_email,admin_photo,admin_address}){
    //     admin_pwd = md5(admin_pwd + AppConfig.md5salt);
    //     let strSql = `insert into ${this.currentTableName}(admin_name,admin_sex,admin_tel,admin_pwd,admin_email,admin_photo,admin_address) value(?,?,?,?,?,?,?)`;
    //     let results = await this.excuteSql(strSql,[admin_name,admin_sex,admin_tel,admin_pwd,admin_email,admin_photo,admin_address])
    //     console.log(results);
    //     return results.affectedRows > 0 ? true : false;
    // }
    async checkLogin({username,password}){
        // console.log(username,password);
        
        let strSql = `select * from ${this.currentTableName} where isDel = false and password = ? `;  
        // password = md5(password + AppConfig.md5salt);
        
        if(/^[0-9A-Za-z]{4,}$/.test(username)){
            strSql += `and username = ?`
            //  console.log(strSql);
        }else if(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(username)){
            strSql += `and email = ?`
        }
        console.log(strSql);
        let results = await this.excuteSql(strSql,[password,username]);
        console.log('results结果',results);
        
        return results[0]
    }
}

module.exports = AdminInfoService;